package com.hxd.otc.utils;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;

/**
 * 包  名 :  com.hxd.otc.utils
 * 类  名 :  CountDownUtils
 * 作  者 :  zjl
 * 时  间 :  2018/6/15 11:52
 * 描  述 :  ${倒计时工具类}
 */
public class CountDownUtils {

    /**
     * 倒计时款操作
     */
    public static void interval(final int count, Observer<Long> observer) {
        if (count < 0)
            return;
        Observable.interval(0, 1, TimeUnit.SECONDS)
                .take(count + 1)
                .map(new Function<Long, Long>() {
                    @Override
                    public Long apply(Long aLong) throws Exception {
                        return count - aLong;
                    }
                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
}
